package org.jeecg.modules.run.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.run.entity.Run;
import org.jeecg.modules.run.entity.RunDetail;
import org.jeecg.modules.run.mapper.RunDetailMapper;
import org.jeecg.modules.run.mapper.RunMapper;
import org.jeecg.modules.run.service.IRunService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 运行批次表
 * @Author: jeecg-boot
 * @Date:   2019-05-24
 * @Version: V1.0
 */
@Slf4j
@Service
public class RunServiceImpl extends ServiceImpl<RunMapper, Run> implements IRunService {

	@Autowired
	private RunMapper runMapper;
	@Autowired
	private RunDetailMapper runDetailMapper;
	
	@Override
	@Transactional
	public void saveMain(Run run, List<RunDetail> runDetailList) {
		runMapper.insert(run);
		for(RunDetail entity:runDetailList) {
			//外键设置
			entity.setFdSscid(run.getId());
			runDetailMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateMain(Run run,List<RunDetail> runDetailList) {
		runMapper.updateById(run);
		
		//1.先删除子表数据
		runDetailMapper.deleteByMainId(run.getId());
		
		//2.子表数据重新插入
		for(RunDetail entity:runDetailList) {
			//外键设置
			entity.setFdSscid(run.getId());
			runDetailMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		runDetailMapper.deleteByMainId(id);
		runMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			runDetailMapper.deleteByMainId(id.toString());
			runMapper.deleteById(id);
		}
	}


	//可能需要业务处理,暂不做成一条sql清理数据
	@Override
	public boolean clearData() {
		try {
			//清理正在运行的监控数据
			clearRunsData();
			//数据监控详情表清理
			clearRunDetaisData();
		} catch (Exception e) {
			log.error("运行监控数据清理失败!",e);
			return false;
		}
		return true;
	}

	void  clearRunDetaisData(){
		List<RunDetail> runDetails = getRunDetailsAry();
		if(null == runDetails || runDetails.size() == 0){
			return;
		}

		for(RunDetail runDetail : runDetails){
			runDetail.setFdStatus(2);
			runDetailMapper.updateById(runDetail);
		}
	}


	void  clearRunsData(){
		List<Run> runs = getRunsAry();
		if(null == runs || runs.size() == 0){
			return;
		}

		for(Run run : runs){
			run.setFdStatus(2);
			runMapper.updateById(run);
		}

	}

	List<Run> getRunsAry(){
		QueryWrapper queryWrapper = new QueryWrapper();
		queryWrapper.eq("fd_status","3");
		return runMapper.selectList(queryWrapper);
	}


	List<RunDetail> getRunDetailsAry(){
		QueryWrapper queryWrapper = new QueryWrapper();
		queryWrapper.eq("fd_status","3");
		return runDetailMapper.selectList(queryWrapper);
	}
}
